package com.itheima.service;

import com.itheima.dao.BaseStudentDao;
import com.itheima.dao.OtherStudentDao;
import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
import com.itheima.factory.StudentDaoFactory;

public class StudentService {
    //OtherStudentDao studentDao = StudentDaoFactory.getStudentDao();
    /*StudentDaoFactory studentDaoFactory = new StudentDaoFactory();
    OtherStudentDao studentDao = studentDaoFactory.getStudentDao();*/
    /*StudentDaoFactory studentDaoFactory = new StudentDaoFactory();
    OtherStudentDao studentDao = studentDaoFactory.getStudentDao();*/
    BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();
    //private  StudentDao studentDao = new StudentDao();

    /**
     * @param student
     * @return 返回flag如果为true表示添加成功, false则表示添加失败
     */
    //添加学生的方法
    public boolean addStudent(Student student) {
        boolean flag = studentDao.addStudent(student);
        return flag;
    }

    //删除学生的方法
    public void deleteStudent(String sid) {
        studentDao.deleteStudent(sid);
    }

    //修改学生的方法
    public void updateStudent(String sid,Student student) {
        studentDao.updateStudent(sid,student);
    }

    //查看学生信息的方法
    public  Student[] showStudent() {

        Student[] stu = studentDao.showStudent();
        //判断stu是否为空数组
        int index = -1;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null){
                index = i;
            }
        }
        if (index == -1){
            stu = null;
        }
        return stu;
    }

    //判断学生学号是否存在的方法
    public boolean isExists(String sid) {
        //可以找到数组里与sid相同的数据返回true
        Student[] students = studentDao.showStudent();
        boolean flag = false;
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && sid.equals(students[i].getSid())) {
                flag = true;
                break;
            } else {
                flag = false;
            }
        }
        return flag;
    }

   /* public boolean findAllStudent(){
    ;

    }*/
    public boolean isNull(String flag){
        if (flag.equals("")){
            return false;
        }
        return true;
    }
}
